#!/bin/bash
mkdir "mark2html-test"
cd "mark2html-test"
cat << 'EOF' > "mark2html"
EOF
mkdir "mark2html-test-0.2"
cd "mark2html-test-0.2"
mkdir "mark2html-test"
cd "mark2html-test"
cat << 'EOF' > "mark2html"
#!/bin/bash

mark() {
	sed -n -e 's/\([^ ]\)  *\([^ ]\)/\1 \2/
	s/^ *$//
	s/^ *\([^ ]\)/\1/
	s/\([^ ]\) *$/\1/
	p' |sed -n -e '1,1 {
:cycle
H
s/^[^\n].*$//
t next
b leave
:next
N
s/\n//
t m
:m
b cycle
:leave
s/^.*\n/^\n/
H
x
s|^\n|<!DOCTYPE html>\n<html>\n<head>\n    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\n    <title>|
s|\([^\n]\)\n*$|\1</title>\n</head>\n<body>|
p
d
}

${
/^ *==* *$/ {
	x
	s/[^\n]/&/
	s/^/<h1>/
	s/<h1>\n*/<h1>/
	s|$|</h1>\n</body>\n</html>|
	p
	d
	}
H
x
s|\*\([^\* ]*\)\*|<em>\1</em>|g
s|^[ \n]*||
s|[ \n]*$||
/[^ \n]/{
	s/^/<p>/
	s|$|</p>\n|
    }
s|$|</body>\n</html>|
p
d
}

/^ *==* *$/! H
/^ *==* *$/{
H
s|^.*$||
x
/[^\n]/{
	s/^/<h1>/
	s/<h1>\n*/<h1>/
	s|\n* *==* *$|</h1>|
	p
	d
    }
} 

/^$/{
H
x
s|\*\([^\* ]*\)\*|<em>\1</em>|g
s|^[ \n]*||
s|[ \n]*$||
/[^\n]/{
	s|^|<p>|
	s|$|</p>|
	p
	d
    }
} 
'
}

fin="/dev/stdin"
fout="/dev/stdout"
while getopts ":i:o:" a 
do 
	case "$a" in
		i)
			fin="$OPTARG"
			;;
		o)
			fout="$OPTARG"
			;;
	esac
done
if [[ $fin == '-' ]] ; then
	fin='/dev/stdin'	
fi
if [[ $fout == '-' ]]; then
	fout='/dev/stdout'

fi
cat "$fin" | mark > "$fout"



EOF
cat << 'EOF' > "test.001.in"
Title001

Heading 1
=========
EOF
cat << 'EOF' > "test.001.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title001</title>
</head>
<body>
<h1>Heading 1</h1>
</body>
</html>
EOF
cat << 'EOF' > "test.002.in"
Title002

  Heading   1    
 =========   
EOF
cat << 'EOF' > "test.002.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title002</title>
</head>
<body>
<h1>Heading 1</h1>
</body>
</html>
EOF
cat << 'EOF' > "test.003.in"
Title003

  Heading   1    
 =========   
   
  
 This is paragraph.

EOF
cat << 'EOF' > "test.003.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title003</title>
</head>
<body>
<h1>Heading 1</h1>
<p>This is paragraph.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.004.in"
Another title

  Heading   1    
 =========   
   
  
 This is paragraph.
And no new line at end.
EOF
cat << 'EOF' > "test.004.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Another title</title>
</head>
<body>
<h1>Heading 1</h1>
<p>This is paragraph.
And no new line at end.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.005.in"
Another title

  Heading   1    
 =========   
 This is paragraph.
And no new line at end.
   
Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.
EOF
cat << 'EOF' > "test.005.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Another title</title>
</head>
<body>
<h1>Heading 1</h1>
<p>This is paragraph.
And no new line at end.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.006.in"
Yet another title

  Heading   1    
 =========   
   
  
 This is paragraph.
And no new line at end.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.

Another Heading
=========================
And paragraph.
EOF
cat << 'EOF' > "test.006.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Yet another title</title>
</head>
<body>
<h1>Heading 1</h1>
<p>This is paragraph.
And no new line at end.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading</h1>
<p>And paragraph.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.007.in"

This is not a title.

Multiline
  heading   1    
 ================== 
   
  
 This is paragraph.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.

Another Heading
=========================
And paragraph.

*Uuuuu~*

Multiple words *are not* em'ed.

EOF
cat << 'EOF' > "test.007.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title></title>
</head>
<body>
<p>This is not a title.</p>
<h1>Multiline
heading 1</h1>
<p>This is paragraph.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em></p>
<p>Multiple words *are not* em'ed.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.008.in"
Title

Multiline
  heading   1    
 ================== 
   
  
 This is paragraph.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.

Another Heading
which is
multiline
   too  
=========================
And paragraph.

*Uuuuu~*

Multiple words *are not* em'ed.

Yet another heading
===================

When 10+10=100?
EOF
cat << 'EOF' > "test.008.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
<h1>Multiline
heading 1</h1>
<p>This is paragraph.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading
which is
multiline
too</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em></p>
<p>Multiple words *are not* em'ed.</p>
<h1>Yet another heading</h1>
<p>When 10+10=100?</p>
</body>
</html>
EOF
cat << 'EOF' > "test.009.in"
Title

Multiline
  heading   1    
  at the end of file
 ================== 
EOF
cat << 'EOF' > "test.009.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
<h1>Multiline
heading 1
at the end of file</h1>
</body>
</html>
EOF
cat << 'EOF' > "test.010.in"
Title

Multiline
  heading   1    
 ================== 
   
  
 This is paragraph.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.

Another Heading
which = is
multiline
   too  
=========================
And paragraph.

*Uuuuu~*

Multiple words *are not* em'ed.

Yet another heading
===================

When 10+10=100?
EOF
cat << 'EOF' > "test.010.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
<h1>Multiline
heading 1</h1>
<p>This is paragraph.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading
which = is
multiline
too</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em></p>
<p>Multiple words *are not* em'ed.</p>
<h1>Yet another heading</h1>
<p>When 10+10=100?</p>
</body>
</html>
EOF
cat << 'EOF' > "test.011.in"
Title

Multiline
  heading   1    
 ================== 
   
  
 This is paragraph.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.


Another Heading
which = is
multiline
   too  
=========================
And paragraph.

*Uuuuu~*

Multiple words *are not* em'ed.

Yet another heading
===================

When 10+10=100?



EOF
cat << 'EOF' > "test.011.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
<h1>Multiline
heading 1</h1>
<p>This is paragraph.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading
which = is
multiline
too</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em></p>
<p>Multiple words *are not* em'ed.</p>
<h1>Yet another heading</h1>
<p>When 10+10=100?</p>
</body>
</html>
EOF
cat << 'EOF' > "test.012.in"
Title

Multiline
 a == a
  heading   1    
 ================== 
  

  Mmmm!



Multiline
 == a
   heading
============
  
 This is paragraph.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.

Another Heading
which = is
multiline
   too  
=========================
And paragraph.

*Uuuuu~*

Multiple words *are not* em'ed.

Yet another heading
===================

When 10+10=100?
EOF
cat << 'EOF' > "test.012.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
<h1>Multiline
a == a
heading 1</h1>
<p>Mmmm!</p>
<h1>Multiline
== a
heading</h1>
<p>This is paragraph.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading
which = is
multiline
too</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em></p>
<p>Multiple words *are not* em'ed.</p>
<h1>Yet another heading</h1>
<p>When 10+10=100?</p>
</body>
</html>
EOF
cat << 'EOF' > "test.013.in"
Title

  *Not* multiline
======================
And paragraph.

*Uuuuu~* uuu!

Multiple words *are not* em'ed.

Yet another heading
===================
EOF
cat << 'EOF' > "test.013.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
<h1>*Not* multiline</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em> uuu!</p>
<p>Multiple words *are not* em'ed.</p>
<h1>Yet another heading</h1>
</body>
</html>
EOF
cat << 'EOF' > "testme.sh"
#!/bin/bash

silent=
if [[ $1 == silent ]]; then
    silent="&>/dev/null"
fi

ex() {
    rm -f "test file" "temp file"
    exit ${1:-1}
}

cp test.001.in "test file"

./mark2html -i "test file" -o "temp file" &>/dev/null 
if ! diff -Nua "test.001.out" "temp file" &>/dev/null; then
    echo "Presentation error" >&2
    ex
fi

cat "test file" | ./mark2html -i - -o "temp file"
if ! diff -Nua "test.001.out" "temp file" &>/dev/null; then
    echo "Presentation error" >&2
    ex
fi

cat "test file" | ./mark2html -i - -o - > "temp file" 
if ! diff -Nua "test.001.out" "temp file" &>/dev/null; then
    echo "Presentation error" >&2
    ex
fi

cat "test file" | ./mark2html > "temp file" 
if ! diff -Nua "test.001.out" "temp file" &>/dev/null; then
    echo "Presentation error" >&2
    ex
fi

wa=0
for a in test.*.in
do
    eval ./mark2html -i "'$a'" -o "'temp file'" $silent
    if ! eval diff -Nua "'${a%.*}.out'" "'temp file'" $silent; then
        echo "Wrong answer, test \`$a'" >&2
        wa=1
    fi
done

rm -f "test file" "temp file"

if ! cat ./mark2html | grep getopt &>/dev/null; then
    echo "getopts command not found but MUST be used. See 'man bash'."
    ex
fi

testfiles=(testme.sh test.*.in test.*.out)
testfiles=${#testfiles[@]}
allfiles=(*)
allfiles=${#allfiles[@]}

(( allfiles == (testfiles + 2) )) && echo "It is not like this is error, but it is better to keep sed script inside main file"
(( allfiles > (testfiles + 2) )) && echo "Hello, Melnikov!" && ex

[[ $wa == 0 ]] && echo "Accepted"
EOF
cd ../
cd ../
mkdir "mark2html-test-0.3"
cd "mark2html-test-0.3"
mkdir "mark2html-test"
cd "mark2html-test"
cat << 'EOF' > "mark2html"
#!/bin/bash

mark() {
	sed -n -e 's/^ //
	p
	${
	s/^.*$/\n\n/
	p}' |sed -n -e '1,1 {
:cycle
H
s/^[^\n].*$//
t next
b leave
:next
N
s/\n//
t m
:m
b cycle
:leave
s/^.*\n/^\n/
H
x
s|^\n|<!DOCTYPE html>\n<html>\n<head>\n    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\n    <title>|
s|\([^\n]\)\n*$|\1</title>\n</head>\n<body>|
p
d
}

s/  */ /g
s/^ //
s/ $//

/^\* /{
s|^\* |<ul><li>|
:list
N
s|\*\([^\* \n][^\* \n]*\)\*|<em>\1</em>|g
s|\[\([^\n\[][^\n\[]*\)\](\([^ \n][^ \n]*\))|<a href="\2">\1</a>|g
s|\[\([^\n\[][^\n\[]*\)\](\([^ \n][^ \n]*\) "\([^"][^"]*\)")|<a href="\2" title="\3">\1</a>|g
/\n$/ {
      s|\n$|</li></ul>|
      p
      d
    }
/\n\* / {
      s|\n\* |\n</li><li>|
    }
b list
}

/^ *==* *$/!{
	/^ *--* *$/! H
}
/^ *==* *$/{
H
s|^.*$||
x
/[^\n]/{
	s/^/<h1>/
	s/<h1>\n*/<h1>/
	s|\n* *==* *$|</h1>|
	$b eof
	p
    }
} 

/^ *--* *$/{
H
s|^.*$||
x
/[^\n]/{
	s/^/<h2>/
	s/<h2>\n*/<h2>/
	s|\n* *--* *$|</h2>|
	$b eof
	p
    }
} 

/^$/{
:paragraph
H
x
s|^\n*||
s|\n*$||
s|\*\([^\* ][^\* ]*\)\*|<em>\1</em>|g
s|\[\([^\n\[][^\n\[]*\)\](\([^ \n][^ \n]*\))|<a href="\2">\1</a>|g
s|\[\([^\n\[][^\n\[]*\)\](\([^ \n][^ \n]*\) "\([^"][^"]*\)")|<a href="\2" title="\3">\1</a>|g
/[^\n]/{
	s|^|<p>|
	s|$|</p>|
	$b eof
	p
    }
$b eof
} 

${
s/^.*$//
b paragraph
:eof
s/\([^\n]\)$/\1\n/
s|$|</body>\n</html>|
p
d
}
'
}

fin="/dev/stdin"
fout="/dev/stdout"
while getopts ":i:o:" a 
do 
	case "$a" in
		i)
			fin="$OPTARG"
			;;
		o)
			fout="$OPTARG"
			;;
	esac
done
if [[ $fin == '-' ]] ; then
	fin='/dev/stdin'	
fi
if [[ $fout == '-' ]]; then
	fout='/dev/stdout'

fi
a=" "$(cat "$fin")
echo "$a" | mark > "$fout"



EOF
cat << 'EOF' > "test.001.in"
Title001

Heading 1
=========
EOF
cat << 'EOF' > "test.001.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title001</title>
</head>
<body>
<h1>Heading 1</h1>
</body>
</html>
EOF
cat << 'EOF' > "test.002.in"
Title002

  Heading   1    
 =========   
EOF
cat << 'EOF' > "test.002.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title002</title>
</head>
<body>
<h1>Heading 1</h1>
</body>
</html>
EOF
cat << 'EOF' > "test.003.in"
Title003

  Heading   1    
 =========   
   
  
 This is paragraph.

EOF
cat << 'EOF' > "test.003.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title003</title>
</head>
<body>
<h1>Heading 1</h1>
<p>This is paragraph.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.004.in"
Another title

  Heading   1    
 =========   
   
  
 This is paragraph.
And no new line at end.
EOF
cat << 'EOF' > "test.004.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Another title</title>
</head>
<body>
<h1>Heading 1</h1>
<p>This is paragraph.
And no new line at end.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.005.in"
Another title

  Heading   1    
 =========   
 This is paragraph.
And no new line at end.
   
Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.
EOF
cat << 'EOF' > "test.005.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Another title</title>
</head>
<body>
<h1>Heading 1</h1>
<p>This is paragraph.
And no new line at end.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.006.in"
Yet another title

  Heading   1    
 =========   
   
  
 This is paragraph.
And no new line at end.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.

Another Heading
=========================
And paragraph.
EOF
cat << 'EOF' > "test.006.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Yet another title</title>
</head>
<body>
<h1>Heading 1</h1>
<p>This is paragraph.
And no new line at end.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading</h1>
<p>And paragraph.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.007.in"

This is not a title.

Multiline
  heading   1    
 ================== 
   
  
 This is paragraph.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.

Another Heading
=========================
And paragraph.

*Uuuuu~*

Multiple words *are not* em'ed.

EOF
cat << 'EOF' > "test.007.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title></title>
</head>
<body>
<p>This is not a title.</p>
<h1>Multiline
heading 1</h1>
<p>This is paragraph.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em></p>
<p>Multiple words *are not* em'ed.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.008.in"
Title

Multiline
  heading   1    
 ================== 
   
  
 This is paragraph.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.

Another Heading
which is
multiline
   too  
=========================
And paragraph.

*Uuuuu~*

Multiple words *are not* em'ed.

Yet another heading
===================

When 10+10=100?
EOF
cat << 'EOF' > "test.008.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
<h1>Multiline
heading 1</h1>
<p>This is paragraph.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading
which is
multiline
too</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em></p>
<p>Multiple words *are not* em'ed.</p>
<h1>Yet another heading</h1>
<p>When 10+10=100?</p>
</body>
</html>
EOF
cat << 'EOF' > "test.009.in"
Title

Multiline
  heading   1    
  at the end of file
 ================== 
EOF
cat << 'EOF' > "test.009.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
<h1>Multiline
heading 1
at the end of file</h1>
</body>
</html>
EOF
cat << 'EOF' > "test.010.in"
Title

Multiline
  heading   1    
 ================== 
   
  
 This is paragraph.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.

Another Heading
which = is
multiline
   too  
=========================
And paragraph.

*Uuuuu~*

Multiple words *are not* em'ed.

Yet another heading
===================

When 10+10=100?
EOF
cat << 'EOF' > "test.010.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
<h1>Multiline
heading 1</h1>
<p>This is paragraph.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading
which = is
multiline
too</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em></p>
<p>Multiple words *are not* em'ed.</p>
<h1>Yet another heading</h1>
<p>When 10+10=100?</p>
</body>
</html>
EOF
cat << 'EOF' > "test.011.in"
Title

Multiline
  heading   1    
 ================== 
   
  
 This is paragraph.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.


Another Heading
which = is
multiline
   too  
=========================
And paragraph.

*Uuuuu~*

Multiple words *are not* em'ed.

Yet another heading
===================

When 10+10=100?



EOF
cat << 'EOF' > "test.011.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
<h1>Multiline
heading 1</h1>
<p>This is paragraph.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading
which = is
multiline
too</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em></p>
<p>Multiple words *are not* em'ed.</p>
<h1>Yet another heading</h1>
<p>When 10+10=100?</p>
</body>
</html>
EOF
cat << 'EOF' > "test.012.in"
Title

Multiline
 a == a
  heading   1    
 ================== 
  

  Mmmm!



Multiline
 == a
   heading
============
  
 This is paragraph.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.

Another Heading
which = is
multiline
   too  
=========================
And paragraph.

*Uuuuu~*

Multiple words *are not* em'ed.

Yet another heading
===================

When 10+10=100?
EOF
cat << 'EOF' > "test.012.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
<h1>Multiline
a == a
heading 1</h1>
<p>Mmmm!</p>
<h1>Multiline
== a
heading</h1>
<p>This is paragraph.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading
which = is
multiline
too</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em></p>
<p>Multiple words *are not* em'ed.</p>
<h1>Yet another heading</h1>
<p>When 10+10=100?</p>
</body>
</html>
EOF
cat << 'EOF' > "test.013.in"
Title

  *Not* multiline
======================
And paragraph.

*Uuuuu~* uuu!

Multiple words *are not* em'ed.

Yet another heading
===================
EOF
cat << 'EOF' > "test.013.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
<h1>*Not* multiline</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em> uuu!</p>
<p>Multiple words *are not* em'ed.</p>
<h1>Yet another heading</h1>
</body>
</html>
EOF
cat << 'EOF' > "test.014.in"
Title *not* em'd and  not *respaced  too*

  *Not* multiline
======================
And paragraph.

*Uuuuu~* uuu!

Multiple words *are not* em'ed.

Yet another heading
===================
EOF
cat << 'EOF' > "test.014.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title *not* em'd and  not *respaced  too*</title>
</head>
<body>
<h1>*Not* multiline</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em> uuu!</p>
<p>Multiple words *are not* em'ed.</p>
<h1>Yet another heading</h1>
</body>
</html>
EOF
cat << 'EOF' > "test.015.in"
Title

*Not* multiline
======================
No letters ** — no em's.
EOF
cat << 'EOF' > "test.015.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
<h1>*Not* multiline</h1>
<p>No letters ** — no em's.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.016.in"
Advanced features

Here goes the fun.

This is first level heading
===========================
Okay. You already have this.

This is second level heading
----------------------------
Okay? Probably not.

Links
-----

Now, why won't we add links? Like [this](http://oxij.livejournal.com/ "somebody's livejournal") or [like this](http://twitter.com/oxij "somebody's twitter") or [even like this](http://google.com/).

Correct examples:
[text (text) text](foo)
[text (text text) text](foo "bar")

Incorrect examples:
[this is text(foo)
[this is text](
[this is text(
[this is text] (foo)
[this is text](foo )
[this is text](foo) "bar")
[this is text](foo "bar)"
[this is text](foo "bar"
[this is text](foo "bar)
[this is text](some shit in between)
[](there "was no text")
[()]()

Lists
-----

Adding full list support with sed is pain.
So, lets do it simple.

* *First* element is nice.
Uuuu~
* [*Second*](http://example.com/) element is cool.
* 3rd [element](http://example.org/) is elegant.
*And this is not an element but it is inside 3rd element*.

* Uwaa~

* Another list!
*cont.*
* Element!

Images
------

I'm too lazy to support this in reference version. Something about this can be found in [wiiwii~'s article on markdown](http://en.wikipedia.org/wiki/Markdown).

Em, why "wiiwii"? [Behold!](http://danbooru.donmai.us/post/show/793404/)

Conclusion
==========
I'm pretty sure this are last new features for this twisted file format.

Rename .out file of this test to html and giggle.
EOF
cat << 'EOF' > "test.016.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Advanced features</title>
</head>
<body>
<p>Here goes the fun.</p>
<h1>This is first level heading</h1>
<p>Okay. You already have this.</p>
<h2>This is second level heading</h2>
<p>Okay? Probably not.</p>
<h2>Links</h2>
<p>Now, why won't we add links? Like <a href="http://oxij.livejournal.com/" title="somebody's livejournal">this</a> or <a href="http://twitter.com/oxij" title="somebody's twitter">like this</a> or <a href="http://google.com/">even like this</a>.</p>
<p>Correct examples:
<a href="foo">text (text) text</a>
<a href="foo" title="bar">text (text text) text</a></p>
<p>Incorrect examples:
[this is text(foo)
[this is text](
[this is text(
[this is text] (foo)
[this is text](foo )
<a href="foo">this is text</a> "bar")
[this is text](foo "bar)"
[this is text](foo "bar"
[this is text](foo "bar)
[this is text](some shit in between)
[](there "was no text")
[()]()</p>
<h2>Lists</h2>
<p>Adding full list support with sed is pain.
So, lets do it simple.</p>
<ul><li><em>First</em> element is nice.
Uuuu~
</li><li><a href="http://example.com/"><em>Second</em></a> element is cool.
</li><li>3rd <a href="http://example.org/">element</a> is elegant.
*And this is not an element but it is inside 3rd element*.</li></ul>
<ul><li>Uwaa~</li></ul>
<ul><li>Another list!
<em>cont.</em>
</li><li>Element!</li></ul>
<h2>Images</h2>
<p>I'm too lazy to support this in reference version. Something about this can be found in <a href="http://en.wikipedia.org/wiki/Markdown">wiiwii~'s article on markdown</a>.</p>
<p>Em, why "wiiwii"? <a href="http://danbooru.donmai.us/post/show/793404/">Behold!</a></p>
<h1>Conclusion</h1>
<p>I'm pretty sure this are last new features for this twisted file format.</p>
<p>Rename .out file of this test to html and giggle.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.017.in"
Title
EOF
cat << 'EOF' > "test.017.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
</body>
</html>
EOF
cat << 'EOF' > "test.018.in"
EOF
cat << 'EOF' > "test.018.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title></title>
</head>
<body>
</body>
</html>
EOF
cat << 'EOF' > "testme.sh"
#!/bin/bash

silent=
if [[ $1 == silent ]]; then
    silent="&>/dev/null"
fi

ex() {
    rm -f "test file" "temp file"
    exit ${1:-1}
}

cp test.001.in "test file"

./mark2html -i "test file" -o "temp file" &>/dev/null 
if ! diff -Nua "test.001.out" "temp file" &>/dev/null; then
    echo "Presentation error 1" >&2
    ex
fi

cat "test file" | ./mark2html -i - -o "temp file"
if ! diff -Nua "test.001.out" "temp file" &>/dev/null; then
    echo "Presentation error 2" >&2
    ex
fi

cat "test file" | ./mark2html -i - -o - > "temp file" 
if ! diff -Nua "test.001.out" "temp file" &>/dev/null; then
    echo "Presentation error 3" >&2
    ex
fi

cat "test file" | ./mark2html > "temp file" 
if ! diff -Nua "test.001.out" "temp file" &>/dev/null; then
    echo "Presentation error 4" >&2
    ex
fi

wa=0
for a in test.*.in
do
    eval ./mark2html -i "'$a'" -o "'temp file'" $silent
    if ! eval diff -Nua "'${a%.*}.out'" "'temp file'" $silent; then
        echo "Wrong answer, test \`$a'" >&2
        wa=1
    fi
done

rm -f "test file" "temp file"

if ! cat ./mark2html | grep getopt &>/dev/null; then
    echo "getopts command not found but MUST be used. See 'man bash'."
    ex
fi

testfiles=(testme.sh test.*.in test.*.out)
testfiles=${#testfiles[@]}
allfiles=(*)
allfiles=${#allfiles[@]}

(( allfiles == (testfiles + 2) )) && echo "It is not like this is error, but it is better to keep sed script inside main file"
(( allfiles > (testfiles + 2) )) && echo "Hello, Melnikov!" && ex

[[ $wa == 0 ]] && echo "Accepted"
EOF
cd ../
cd ../
mkdir "mark2html-test-0.4"
cd "mark2html-test-0.4"
mkdir "mark2html-test"
cd "mark2html-test"
cat << 'EOF' > "mark2html"
#!/bin/bash

mark() {
	sed -n -e 's/^ //
	p
	${
	s/^.*$/\n\n/
	p}' |sed -n -e '1,1 {
:cycle
H
s/^[^\n].*$//
t next
b leave
:next
N
s/\n//
t m
:m
b cycle
:leave
s/^.*\n/^\n/
H
x
s|^\n|<!DOCTYPE html>\n<html>\n<head>\n    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\n    <title>|
s|\([^\n]\)\n*$|\1</title>\n</head>\n<body>|
p
d
}

s/  */ /g
s/^ //
s/ $//

/^\* /{
b paragraph
:listret
s|^\* |<ul><li>|
:list
N
s|\*\([^\* \n][^\* \n]*\)\*|<em>\1</em>|g
s|\[\([^\n\[][^\n\[]*\)\](\([^ \n][^ \n]*\))|<a href="\2">\1</a>|g
s|\[\([^\n\[][^\n\[]*\)\](\([^ \n][^ \n]*\) "\([^"][^"]*\)")|<a href="\2" title="\3">\1</a>|g
/\n$/ {
      s|\n$|</li></ul>|
      p
      d
    }
/\n\* / {
      s|\n\* |\n</li><li>|
    }
b list
}

/^ *==* *$/!{
	/^ *--* *$/! H
}
/^ *==* *$/{
H
s|^.*$||
x
/[^\n]/{
	s/^/<h1>/
	s/<h1>\n*/<h1>/
	s|\n* *==* *$|</h1>|
	$b eof
	p
    }
} 

/^ *--* *$/{
H
s|^.*$||
x
/[^\n]/{
	s/^/<h2>/
	s/<h2>\n*/<h2>/
	s|\n* *--* *$|</h2>|
	$b eof
	p
    }
} 

/^$/{
H
:paragraph
x
s|^\n*||
s|\n*$||
s|\*\([^\* ][^\* ]*\)\*|<em>\1</em>|g
s|\[\([^\n\[][^\n\[]*\)\](\([^ \n][^ \n]*\))|<a href="\2">\1</a>|g
s|\[\([^\n\[][^\n\[]*\)\](\([^ \n][^ \n]*\) "\([^"][^"]*\)")|<a href="\2" title="\3">\1</a>|g
/[^\n]/{
	s|^|<p>|
	s|$|</p>|
	x
	/^\* /{
	    x
	    p
	    s/^.*$//
	    x
	    b listret
        }
	x
	$b eof
	p
    }
x
/^\* /b listret
x
$b eof
} 

${
s/^.*$//
H
b paragraph
:eof
s/\([^\n]\)$/\1\n/
s|$|</body>\n</html>|
p
d
}
'
}

fin="/dev/stdin"
fout="/dev/stdout"
while getopts ":i:o:" a 
do 
	case "$a" in
		i)
			fin="$OPTARG"
			;;
		o)
			fout="$OPTARG"
			;;
	esac
done
if [[ $fin == '-' ]] ; then
	fin='/dev/stdin'	
fi
if [[ $fout == '-' ]]; then
	fout='/dev/stdout'

fi
a=" "$(cat "$fin")
echo "$a" | mark > "$fout"



EOF
cat << 'EOF' > "test.001.in"
Title001

Heading 1
=========
EOF
cat << 'EOF' > "test.001.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title001</title>
</head>
<body>
<h1>Heading 1</h1>
</body>
</html>
EOF
cat << 'EOF' > "test.002.in"
Title002

  Heading   1    
 =========   
EOF
cat << 'EOF' > "test.002.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title002</title>
</head>
<body>
<h1>Heading 1</h1>
</body>
</html>
EOF
cat << 'EOF' > "test.003.in"
Title003

  Heading   1    
 =========   
   
  
 This is paragraph.

EOF
cat << 'EOF' > "test.003.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title003</title>
</head>
<body>
<h1>Heading 1</h1>
<p>This is paragraph.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.004.in"
Another title

  Heading   1    
 =========   
   
  
 This is paragraph.
And no new line at end.
EOF
cat << 'EOF' > "test.004.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Another title</title>
</head>
<body>
<h1>Heading 1</h1>
<p>This is paragraph.
And no new line at end.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.005.in"
Another title

  Heading   1    
 =========   
 This is paragraph.
And no new line at end.
   
Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.
EOF
cat << 'EOF' > "test.005.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Another title</title>
</head>
<body>
<h1>Heading 1</h1>
<p>This is paragraph.
And no new line at end.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.006.in"
Yet another title

  Heading   1    
 =========   
   
  
 This is paragraph.
And no new line at end.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.

Another Heading
=========================
And paragraph.
EOF
cat << 'EOF' > "test.006.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Yet another title</title>
</head>
<body>
<h1>Heading 1</h1>
<p>This is paragraph.
And no new line at end.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading</h1>
<p>And paragraph.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.007.in"

This is not a title.

Multiline
  heading   1    
 ================== 
   
  
 This is paragraph.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.

Another Heading
=========================
And paragraph.

*Uuuuu~*

Multiple words *are not* em'ed.

EOF
cat << 'EOF' > "test.007.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title></title>
</head>
<body>
<p>This is not a title.</p>
<h1>Multiline
heading 1</h1>
<p>This is paragraph.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em></p>
<p>Multiple words *are not* em'ed.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.008.in"
Title

Multiline
  heading   1    
 ================== 
   
  
 This is paragraph.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.

Another Heading
which is
multiline
   too  
=========================
And paragraph.

*Uuuuu~*

Multiple words *are not* em'ed.

Yet another heading
===================

When 10+10=100?
EOF
cat << 'EOF' > "test.008.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
<h1>Multiline
heading 1</h1>
<p>This is paragraph.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading
which is
multiline
too</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em></p>
<p>Multiple words *are not* em'ed.</p>
<h1>Yet another heading</h1>
<p>When 10+10=100?</p>
</body>
</html>
EOF
cat << 'EOF' > "test.009.in"
Title

Multiline
  heading   1    
  at the end of file
 ================== 
EOF
cat << 'EOF' > "test.009.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
<h1>Multiline
heading 1
at the end of file</h1>
</body>
</html>
EOF
cat << 'EOF' > "test.010.in"
Title

Multiline
  heading   1    
 ================== 
   
  
 This is paragraph.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.

Another Heading
which = is
multiline
   too  
=========================
And paragraph.

*Uuuuu~*

Multiple words *are not* em'ed.

Yet another heading
===================

When 10+10=100?
EOF
cat << 'EOF' > "test.010.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
<h1>Multiline
heading 1</h1>
<p>This is paragraph.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading
which = is
multiline
too</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em></p>
<p>Multiple words *are not* em'ed.</p>
<h1>Yet another heading</h1>
<p>When 10+10=100?</p>
</body>
</html>
EOF
cat << 'EOF' > "test.011.in"
Title

Multiline
  heading   1    
 ================== 
   
  
 This is paragraph.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.


Another Heading
which = is
multiline
   too  
=========================
And paragraph.

*Uuuuu~*

Multiple words *are not* em'ed.

Yet another heading
===================

When 10+10=100?



EOF
cat << 'EOF' > "test.011.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
<h1>Multiline
heading 1</h1>
<p>This is paragraph.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading
which = is
multiline
too</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em></p>
<p>Multiple words *are not* em'ed.</p>
<h1>Yet another heading</h1>
<p>When 10+10=100?</p>
</body>
</html>
EOF
cat << 'EOF' > "test.012.in"
Title

Multiline
 a == a
  heading   1    
 ================== 
  

  Mmmm!



Multiline
 == a
   heading
============
  
 This is paragraph.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.

Another Heading
which = is
multiline
   too  
=========================
And paragraph.

*Uuuuu~*

Multiple words *are not* em'ed.

Yet another heading
===================

When 10+10=100?
EOF
cat << 'EOF' > "test.012.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
<h1>Multiline
a == a
heading 1</h1>
<p>Mmmm!</p>
<h1>Multiline
== a
heading</h1>
<p>This is paragraph.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading
which = is
multiline
too</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em></p>
<p>Multiple words *are not* em'ed.</p>
<h1>Yet another heading</h1>
<p>When 10+10=100?</p>
</body>
</html>
EOF
cat << 'EOF' > "test.013.in"
Title

  *Not* multiline
======================
And paragraph.

*Uuuuu~* uuu!

Multiple words *are not* em'ed.

Yet another heading
===================
EOF
cat << 'EOF' > "test.013.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
<h1>*Not* multiline</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em> uuu!</p>
<p>Multiple words *are not* em'ed.</p>
<h1>Yet another heading</h1>
</body>
</html>
EOF
cat << 'EOF' > "test.014.in"
Title *not* em'd and  not *respaced  too*

  *Not* multiline
======================
And paragraph.

*Uuuuu~* uuu!

Multiple words *are not* em'ed.

Yet another heading
===================
EOF
cat << 'EOF' > "test.014.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title *not* em'd and  not *respaced  too*</title>
</head>
<body>
<h1>*Not* multiline</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em> uuu!</p>
<p>Multiple words *are not* em'ed.</p>
<h1>Yet another heading</h1>
</body>
</html>
EOF
cat << 'EOF' > "test.015.in"
Title

*Not* multiline
======================
No letters ** — no em's.
EOF
cat << 'EOF' > "test.015.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
<h1>*Not* multiline</h1>
<p>No letters ** — no em's.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.016.in"
Advanced features

Here goes the fun.

This is first level heading
===========================
Okay. You already have this.

This is second level heading
----------------------------
Okay? Probably not.

Links
-----

Now, why won't we add links? Like [this](http://oxij.livejournal.com/ "somebody's livejournal") or [like this](http://twitter.com/oxij "somebody's twitter") or [even like this](http://google.com/).

Correct examples:
[text (text) text](foo)
[text (text text) text](foo "bar")

Incorrect examples:
[this is text(foo)
[this is text](
[this is text(
[this is text] (foo)
[this is text](foo )
[this is text](foo) "bar")
[this is text](foo "bar)"
[this is text](foo "bar"
[this is text](foo "bar)
[this is text](some shit in between)
[](there "was no text")
[()]()

Images
------

I'm too lazy to support this in reference version. Something about this can be found in [wiiwii~'s article on markdown](http://en.wikipedia.org/wiki/Markdown).

Em, why "wiiwii"? [Behold!](http://danbooru.donmai.us/post/show/793404/)

Conclusion
==========
I'm pretty sure this are last new features for this twisted file format.

Rename .out file of this test to html and giggle.
EOF
cat << 'EOF' > "test.016.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Advanced features</title>
</head>
<body>
<p>Here goes the fun.</p>
<h1>This is first level heading</h1>
<p>Okay. You already have this.</p>
<h2>This is second level heading</h2>
<p>Okay? Probably not.</p>
<h2>Links</h2>
<p>Now, why won't we add links? Like <a href="http://oxij.livejournal.com/" title="somebody's livejournal">this</a> or <a href="http://twitter.com/oxij" title="somebody's twitter">like this</a> or <a href="http://google.com/">even like this</a>.</p>
<p>Correct examples:
<a href="foo">text (text) text</a>
<a href="foo" title="bar">text (text text) text</a></p>
<p>Incorrect examples:
[this is text(foo)
[this is text](
[this is text(
[this is text] (foo)
[this is text](foo )
<a href="foo">this is text</a> "bar")
[this is text](foo "bar)"
[this is text](foo "bar"
[this is text](foo "bar)
[this is text](some shit in between)
[](there "was no text")
[()]()</p>
<h2>Images</h2>
<p>I'm too lazy to support this in reference version. Something about this can be found in <a href="http://en.wikipedia.org/wiki/Markdown">wiiwii~'s article on markdown</a>.</p>
<p>Em, why "wiiwii"? <a href="http://danbooru.donmai.us/post/show/793404/">Behold!</a></p>
<h1>Conclusion</h1>
<p>I'm pretty sure this are last new features for this twisted file format.</p>
<p>Rename .out file of this test to html and giggle.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.017.in"
Title
EOF
cat << 'EOF' > "test.017.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>
</body>
</html>
EOF
cat << 'EOF' > "test.018.in"
EOF
cat << 'EOF' > "test.018.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title></title>
</head>
<body>
</body>
</html>
EOF
cat << 'EOF' > "test.019.in"
Advanced features (cont.)

Lists
-----

Adding full list support with sed is pain.
So, lets do it simple.

* Uwaa~

* Another list!
*cont.*
* Element!

Uuuu~
* *First* element is nice.
* [*Second*](http://example.com/) element is cool.
* 3rd [element](http://example.org/) is elegant.
*And this is not an element but it is inside 3rd element*.

Uuuu~
* *First* element is nice.
* [*Second*](http://example.com/) element is cool.
* 3rd [element](http://example.org/) is elegant.
*And this is not an element but it is inside 3rd element*.
EOF
cat << 'EOF' > "test.019.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Advanced features (cont.)</title>
</head>
<body>
<h2>Lists</h2>
<p>Adding full list support with sed is pain.
So, lets do it simple.</p>
<ul><li>Uwaa~</li></ul>
<ul><li>Another list!
<em>cont.</em>
</li><li>Element!</li></ul>
<p>Uuuu~</p>
<ul><li><em>First</em> element is nice.
</li><li><a href="http://example.com/"><em>Second</em></a> element is cool.
</li><li>3rd <a href="http://example.org/">element</a> is elegant.
*And this is not an element but it is inside 3rd element*.</li></ul>
<p>Uuuu~</p>
<ul><li><em>First</em> element is nice.
</li><li><a href="http://example.com/"><em>Second</em></a> element is cool.
</li><li>3rd <a href="http://example.org/">element</a> is elegant.
*And this is not an element but it is inside 3rd element*.</li></ul>
</body>
</html>
EOF
cat << 'EOF' > "testme.sh"
#!/bin/bash

silent=
if [[ $1 == silent ]]; then
    silent="&>/dev/null"
fi

ex() {
    rm -f "test file" "temp file"
    exit ${1:-1}
}

cp test.001.in "test file"

./mark2html -i "test file" -o "temp file" &>/dev/null 
if [[ $? != 0 ]] || [[ ! -e "temp file" ]]; then
    echo "Presentation error (1)" >&2
    ex
fi
rm "temp file"

cat "test file" | ./mark2html -i - -o "temp file"
if [[ $? != 0 ]] || [[ ! -e "temp file" ]]; then
    echo "Presentation error (2)" >&2
    ex
fi
rm "temp file"

cat "test file" | ./mark2html -i - -o - > "temp file" 
if [[ $? != 0 ]] || [[ ! -e "temp file" ]]; then
    echo "Presentation error (3)" >&2
    ex
fi
rm "temp file"

cat "test file" | ./mark2html > "temp file" 
if [[ $? != 0 ]] || [[ ! -e "temp file" ]]; then
    echo "Presentation error (4)" >&2
    ex
fi
rm "temp file"

wa=0
for a in test.*.in
do
    eval ./mark2html -i "'$a'" -o "'temp file'" $silent
    if ! eval diff -Nua "'${a%.*}.out'" "'temp file'" $silent; then
        echo "Wrong answer, test \`$a'" >&2
        if [[ $wa != 1 ]] && [[ $a == "test.019.in" ]]; then
            wa=2
        else
            wa=1
        fi
    fi
done

rm -f "test file" "temp file"

if ! cat ./mark2html | grep getopt &>/dev/null; then
    echo "getopts command not found but MUST be used. See 'man bash'."
    ex
fi

testfiles=(testme.sh test.*.in test.*.out)
testfiles=${#testfiles[@]}
allfiles=(*)
allfiles=${#allfiles[@]}

(( allfiles == (testfiles + 2) )) && echo "It is not like this is an error, but it is better to keep sed script inside main file"
(( allfiles > (testfiles + 2) )) && echo "Hello, Melnikov!" && ex

[[ $wa == 0 ]] && echo "Accepted"
[[ $wa == 1 ]] && echo "Rejected"
[[ $wa == 2 ]] && echo "Partially accepted"
EOF
cd ../
cd ../
cat << 'EOF' > "mark2html-tmpl"
#!/bin/bash
# -*- coding: utf-8 -*-
# Copyright (C) 2010 Jan Malakhovski

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

mark_sed() {
    sed -n '
s|  *| |g
s|^ \(.*\)$|\1|
s|^\(.*\) $|\1|
p
'
}

input="/dev/stdin"
output="/dev/stdout"

while getopts ":i:o:" opt; do
    case $opt in
    i)
        input="$OPTARG";
        ;;
    o)
        output="$OPTARG";
        ;;
    \?)
        echo "Invalid option: -$OPTARG" >&2
        exit 1
        ;;
  esac
done

cat "$input" | mark_sed > "$output"
EOF
cat << 'EOF' > "s.sh"
#!/bin/bash
mkdir "mark2html-test"
cd "mark2html-test"
cd ../
mkdir "repo"
cd "repo"
cd ../
EOF
cat << 'EOF' > "test.001.in"
Title001

Heading 1
=========
EOF
cat << 'EOF' > "test.001.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title001</title>
</head>
<body>
<h1>Heading 1</h1>
</body>
</html>
EOF
cat << 'EOF' > "test.002.in"
Title002

  Heading   1    
 =========   
EOF
cat << 'EOF' > "test.002.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title002</title>
</head>
<body>
<h1>Heading 1</h1>
</body>
</html>
EOF
cat << 'EOF' > "test.003.in"
Title003

  Heading   1    
 =========   
   
  
 This is paragraph.

EOF
cat << 'EOF' > "test.003.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title003</title>
</head>
<body>
<h1>Heading 1</h1>
<p>This is paragraph.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.004.in"
Another title

  Heading   1    
 =========   
   
  
 This is paragraph.
And no new line at end.
EOF
cat << 'EOF' > "test.004.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Another title</title>
</head>
<body>
<h1>Heading 1</h1>
<p>This is paragraph.
And no new line at end.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.005.in"
Another title

  Heading   1    
 =========   
 This is paragraph.
And no new line at end.
   
Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.
EOF
cat << 'EOF' > "test.005.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Another title</title>
</head>
<body>
<h1>Heading 1</h1>
<p>This is paragraph.
And no new line at end.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.006.in"
Yet another title

  Heading   1    
 =========   
   
  
 This is paragraph.
And no new line at end.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.

Another Heading
=========================
And paragraph.
EOF
cat << 'EOF' > "test.006.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Yet another title</title>
</head>
<body>
<h1>Heading 1</h1>
<p>This is paragraph.
And no new line at end.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading</h1>
<p>And paragraph.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.007.in"

This is not a title.

Multiline
  heading   1    
 ================== 
   
  
 This is paragraph.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.

Another Heading
=========================
And paragraph.

*Uuuuu~*

Multiple words *are not* em'ed.

EOF
cat << 'EOF' > "test.007.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title></title>
</head>
<body>
<p>This is not a title.</p>
<h1>Multiline
heading 1</h1>
<p>This is paragraph.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em></p>
<p>Multiple words *are not* em'ed.</p>
</body>
</html>
EOF
cat << 'EOF' > "testme.sh"
#!/bin/bash

ex() {
    rm -f "test file" "temp file"
    exit ${1:-1}
}

cp test.001.in "test file"

./mark2html -i "test file" -o "temp file"
if ! diff -Nua "test.001.out" "temp file" &>/dev/null; then
    echo "Presentation error" >&2
    ex
fi

cat "test file" | ./mark2html -i - -o "temp file"
if ! diff -Nua "test.001.out" "temp file" &>/dev/null; then
    echo "Presentation error" >&2
    ex
fi

cat "test file" | ./mark2html -i - -o - > "temp file" 
if ! diff -Nua "test.001.out" "temp file" &>/dev/null; then
    echo "Presentation error" >&2
    ex
fi

cat "test file" | ./mark2html > "temp file" 
if ! diff -Nua "test.001.out" "temp file" &>/dev/null; then
    echo "Presentation error" >&2
    ex
fi

for a in test.*.in
do
    ./mark2html -i "$a" -o "temp file"
    if ! diff -Nua "${a%.*}.out" "temp file"; then
        echo "Wrong answer, test \`$a'" >&2
        ex
    fi
done

echo "Accepted"
ex 0
EOF
mkdir "новая папка"
cd "новая папка"
cat << 'EOF' > "mark2html"
#!/bin/bash

mark() {
	sed -n -e 's/\([^ ]\)  *\([^ ]\)/\1 \2/
	s/^ *$//
	s/^ *\([^ ]\)/\1/
	s/\([^ ]\) *$/\1/
	s|\*\([^\* ]*\)\*|<em>\1</em>|g
	p' |sed -n -f "marksed"  
}

fin="/dev/stdin"
fout="/dev/stdout"
while getopts ":i:o:" a 
do 
	case "$a" in
		i)
			fin="$OPTARG"
			;;
		o)
			fout="$OPTARG"
			;;
	esac
done
if [[ $fin == '-' ]] ; then
	fin='/dev/stdin'	
fi
if [[ $fout == '-' ]]; then
	fout='/dev/stdout'

fi
cat "$fin" | mark > "$fout"



EOF
cat << 'EOF' > "marksed"
1,1 {
:cycle
H
s/^[^\n].*$//
t next
b leave
:next
N
s/\n//
t m
:m
b cycle
:leave
s/^.*\n/^\n/
H
x
s|^\n|<!DOCTYPE html>\n<html>\n<head>\n    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\n    <title>|
s|\([^\n]\)\n*$|\1</title>\n</head>\n<body>|
p
d
}

${
/^ *==* *$/ {
	x
	s/[^\n]/&/
	s/^/<h1>/
	s/<h1>\n*/<h1>/
	s|$|</h1>\n</body>\n</html>|
	p
	d
	}
H
x
s/[^\n]/&/
s/^[\n* *]*\([^\n]\)/<p>\1/
s|\([^\n]\)\n*$|\1</p>\n</body>\n</html>|
p
d
}

/^ *==* *$/! H
/^ *==* *$/{
H
s|^.*$||
x
s/[^\n]/&/
t lable2
d
:lable2
s/^/<h1>/
s/<h1>\n*/<h1>/
s|\n* *==* *$|</h1>|
p
d
} 

/^$/{
H
x
s/[^\n]/&/
t lable
d
:lable
s/^\n*\([^\n]\)/<p>\1/
s|\n$|</p>|
s|\([^\n]\)\n*</p>|\1</p>|
p
} 

EOF
cat << 'EOF' > "test.001.in"
Title001

Heading 1
=========
EOF
cat << 'EOF' > "test.001.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title001</title>
</head>
<body>
<h1>Heading 1</h1>
</body>
</html>
EOF
cat << 'EOF' > "test.002.in"
Title002

  Heading   1    
 =========   
EOF
cat << 'EOF' > "test.002.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title002</title>
</head>
<body>
<h1>Heading 1</h1>
</body>
</html>
EOF
cat << 'EOF' > "test.003.in"
Title003

  Heading   1    
 =========   
   
  
 This is paragraph.

EOF
cat << 'EOF' > "test.003.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title003</title>
</head>
<body>
<h1>Heading 1</h1>
<p>This is paragraph.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.004.in"
Another title

  Heading   1    
 =========   
   
  
 This is paragraph.
And no new line at end.
EOF
cat << 'EOF' > "test.004.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Another title</title>
</head>
<body>
<h1>Heading 1</h1>
<p>This is paragraph.
And no new line at end.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.005.in"
Another title

  Heading   1    
 =========   
 This is paragraph.
And no new line at end.
   
Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.
EOF
cat << 'EOF' > "test.005.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Another title</title>
</head>
<body>
<h1>Heading 1</h1>
<p>This is paragraph.
And no new line at end.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.006.in"
Yet another title

  Heading   1    
 =========   
   
  
 This is paragraph.
And no new line at end.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.

Another Heading
=========================
And paragraph.
EOF
cat << 'EOF' > "test.006.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Yet another title</title>
</head>
<body>
<h1>Heading 1</h1>
<p>This is paragraph.
And no new line at end.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading</h1>
<p>And paragraph.</p>
</body>
</html>
EOF
cat << 'EOF' > "test.007.in"

This is not a title.

Multiline
  heading   1    
 ================== 
   
  
 This is paragraph.

Another paragraph.
Lalalalal. La-la-la.
Here we go.
One *em* tag.
And two *em* *tags*.
And *three* em *tags* at *one* line.

Another Heading
=========================
And paragraph.

*Uuuuu~*

Multiple words *are not* em'ed.

EOF
cat << 'EOF' > "test.007.out"
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title></title>
</head>
<body>
<p>This is not a title.</p>
<h1>Multiline
heading 1</h1>
<p>This is paragraph.</p>
<p>Another paragraph.
Lalalalal. La-la-la.
Here we go.
One <em>em</em> tag.
And two <em>em</em> <em>tags</em>.
And <em>three</em> em <em>tags</em> at <em>one</em> line.</p>
<h1>Another Heading</h1>
<p>And paragraph.</p>
<p><em>Uuuuu~</em></p>
<p>Multiple words *are not* em'ed.</p>
</body>
</html>
EOF
cat << 'EOF' > "testme.sh"
#!/bin/bash

ex() {
    rm -f "test file" "temp file"
    exit ${1:-1}
}

cp test.001.in "test file"

./mark2html -i "test file" -o "temp file"
if ! diff -Nua "test.001.out" "temp file" &>/dev/null; then
    echo "Presentation error" >&2
    ex
fi

cat "test file" | ./mark2html -i - -o "temp file"
if ! diff -Nua "test.001.out" "temp file" &>/dev/null; then
    echo "Presentation error" >&2
    ex
fi

cat "test file" | ./mark2html -i - -o - > "temp file" 
if ! diff -Nua "test.001.out" "temp file" &>/dev/null; then
    echo "Presentation error" >&2
    ex
fi

cat "test file" | ./mark2html > "temp file" 
if ! diff -Nua "test.001.out" "temp file" &>/dev/null; then
    echo "Presentation error" >&2
    ex
fi

for a in test.*.in
do
    ./mark2html -i "$a" -o "temp file"
    if ! diff -Nua "${a%.*}.out" "temp file"; then
        echo "Wrong answer, test \`$a'" >&2
        ex
    fi
done

echo "Accepted"
ex 0
EOF
cd ../
cd ../
